package com.example.a313ldy.yuejian_17.Model;

import com.example.a313ldy.yuejian_17.Bean.Train;
import com.example.a313ldy.yuejian_17.Listener.TrainListener;
import com.example.a313ldy.yuejian_17.Service.TrainService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by LDY on 2017/10/19.
 */

public class TrainModel {
    private TrainService service;
    private Retrofit retrofit;

    public TrainModel() {
        retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl("http://123.207.74.206/yuejian/api.php/")
                .build();

    }





    public void TrainData(final TrainListener<List<Train>> listener,int flag) {
        service= retrofit.create(TrainService.class);
        Call<List<Train>> call = service.trainlist(flag);
        //Retrofit使用-3
        call.enqueue(new Callback<List<Train>>() {
            @Override
            public void onResponse(Call<List<Train>>call, Response<List<Train>> response) {
                if(response.isSuccessful()&&response.body()!=null){
                    //调用监听事件接口的onResponse方法
                    listener.onResponse(response.body());
                }
                else{
                    listener.onFail("解析错误！");
                }
            }

            @Override
            public void onFailure(Call<List<Train>>call, Throwable t) {
                //调用监听事件接口的onFail方法
                listener.onFail(t.toString());
            }
        });
    }


}
